package com.wx.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.wx.common.redis.RedisCacheHand;
import com.wx.common.redis.RedisUtil;
import com.wx.entity.Address;
import com.wx.entity.GoodsPrice;
import com.wx.entity.GoodsProperty;
import com.wx.entity.GoodsProperties;
import com.wx.mapper.AddressMapper;
import com.wx.mapper.GoodsPriceMapper;
import com.wx.mapper.GoodsPropertyMapper;
import com.wx.mapper.GoodsPropertiesMapper;
import com.wx.service.CommonService;

@Service
public class CommonServiceImpl implements CommonService {

	private static final Logger logger = LoggerFactory.getLogger(CommonServiceImpl.class);
	
	@Autowired
	private RedisUtil redisUtil;
	
	@Autowired
	private AddressMapper addressMapper;
	
	@Autowired
	private GoodsPropertiesMapper goodsPropertiesMapper;
	
	@Autowired
	private GoodsPropertyMapper goodPropertyMapper;
	
	@Autowired
	private GoodsPriceMapper goodsPriceMapper;
	
	@Override
	public void insert(Address record) {
		addressMapper.insert(record);
	}

	@Override
	public List<Address> selectAllByLevel(Integer level) {
		return addressMapper.selectAllByLevel(level);
	}

	@Override
	public List<Address> selectAllByPid(String pid) {
		return addressMapper.selectAllByPid(pid);
	}

	@Override
	public void initAddressRedisCache() {
		String flag = redisUtil.get("Address:isOrNotHas");
		if(flag != null && flag.equals("true")) {
			logger.info("AddressRedisCache已存在");
		} else {
			logger.info("AddressRedisCache不存在，创建redis缓存");
			List<Address> list = addressMapper.selectIdWithName();
			if(list.size() != 0) {
				for(Address item : list) {
					redisUtil.set(RedisCacheHand.AddressCache + item.getId(), item.getName());
				}
				redisUtil.set("Address:isOrNotHas", "true");
			}
			logger.info("AddressRedisCache缓存创建成功");
		}
	}

	@Override
	public void initGoodsPropertiesCache() {
		String flag = redisUtil.get("GoodsProperties:isOrNotHas");
		if(flag != null && flag.equals("true")) {
			logger.info("GoodsPropertiesRedisCache已存在");
		} else {
			logger.info("GoodsPropertiesRedisCache不存在，创建redis缓存");
			List<GoodsProperties> properties = goodsPropertiesMapper.selectAll();
			if(properties.size() != 0) {
				for(GoodsProperties item : properties) {
					redisUtil.set(RedisCacheHand.GoodsPropertiesCache + item.getId(), JSON.toJSONString(item));
				}
				redisUtil.set("GoodsProperties:isOrNotHas", "true");
			}
			logger.info("GoodsPropertiesRedisCache缓存创建成功");
		}
	}

	@Override
	public void initGoodsPropertyCache() {
		List<GoodsProperty> list = goodPropertyMapper.selectAll();
		for(GoodsProperty item : list) {
			redisUtil.set(RedisCacheHand.GoodsPropertyCache + item.getGoodsId() + item.getPropertyF(), item.getPropertyC());
		}
		logger.info("GoodsPropertyCache缓存创建成功");
	}

	@Override
	public void initGoodsPriceCache() {
		List<GoodsPrice> list = goodsPriceMapper.selectAll();
		for(GoodsPrice item : list) {
			redisUtil.set(RedisCacheHand.GoodsPriceCache + item.getGoodsId() + ":" + item.getPropertychildids(), JSON.toJSONString(item));
		}
		logger.info("GoodsPriceCache缓存创建成功");
	}

}
